package com.example.project.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.project.core.text.Convert;
import com.example.project.entity.Watcher;
import com.example.project.mapper.WatcherMapper;
import com.example.project.service.WatcherService;
import com.example.project.utils.DateUtils;

/**
 * 用户关注Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-06
 */
@Service
public class WatcherServiceImpl implements WatcherService {
	
	@Autowired
	private WatcherMapper watcherMapper;

	/**
	 * 查询用户关注
	 * 
	 * @param id 用户关注主键
	 * @return 用户关注
	 */
	@Override
	public Watcher selectWatcherById(Long id) {
		return watcherMapper.selectWatcherById(id);
	}

	/**
	 * 查询用户关注列表
	 * 
	 * @param watcher 用户关注
	 * @return 用户关注
	 */
	@Override
	public List<Watcher> selectWatcherList(Watcher watcher) {
		return watcherMapper.selectWatcherList(watcher);
	}

	/**
	 * 新增用户关注
	 * 
	 * @param watcher 用户关注
	 * @return 结果
	 */
	@Override
	public int insertWatcher(Watcher watcher) {
		return watcherMapper.insertWatcher(watcher);
	}

	/**
	 * 修改用户关注
	 * 
	 * @param watcher 用户关注
	 * @return 结果
	 */
	@Override
	public int updateWatcher(Watcher watcher) {
		return watcherMapper.updateWatcher(watcher);
	}

	/**
	 * 批量删除用户关注
	 * 
	 * @param ids 需要删除的用户关注主键
	 * @return 结果
	 */
	@Override
	public int deleteWatcherByIds(String ids) {
		return watcherMapper.deleteWatcherByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除用户关注信息
	 * 
	 * @param id 用户关注主键
	 * @return 结果
	 */
	@Override
	public int deleteWatcherById(Long id) {
		return watcherMapper.deleteWatcherById(id);
	}
	
	/**
	 * 关注某个对象
	 * 
	 * @param watchableType 被关注对象类型
	 * @param watchableId 被关注对象ID
	 * @param userId 用户ID
	 * @return 结果
	 */
	@Override
	public boolean watch(String watchableType, Long watchableId, Long userId) {
		// 检查是否已经关注
		Watcher existingWatcher = watcherMapper.selectWatcherByConditions(watchableType, watchableId, userId);
		if (existingWatcher != null) {
			return true; // 已经关注，返回成功
		}
		
		// 创建新的关注记录
		Watcher watcher = new Watcher();
		watcher.setWatchableType(watchableType);
		watcher.setWatchableId(watchableId);
		watcher.setUserId(userId);
		watcher.setCreateTime(DateUtils.curLocalDateTime());
		watcher.setUpdateTime(DateUtils.curLocalDateTime());
		
		int result = watcherMapper.insertWatcher(watcher);
		return result > 0;
	}
	
	/**
	 * 取消关注某个对象
	 * 
	 * @param watchableType 被关注对象类型
	 * @param watchableId 被关注对象ID
	 * @param userId 用户ID
	 * @return 结果
	 */
	@Override
	public boolean unwatch(String watchableType, Long watchableId, Long userId) {
		int result = watcherMapper.deleteWatcherByConditions(watchableType, watchableId, userId);
		return result > 0;
	}
	
	/**
	 * 检查用户是否关注了某个对象
	 * 
	 * @param watchableType 被关注对象类型
	 * @param watchableId 被关注对象ID
	 * @param userId 用户ID
	 * @return 是否关注
	 */
	@Override
	public boolean isWatched(String watchableType, Long watchableId, Long userId) {
		Watcher watcher = watcherMapper.selectWatcherByConditions(watchableType, watchableId, userId);
		return watcher != null;
	}
	
	/**
	 * 获取某个对象的关注者列表
	 * 
	 * @param watchableType 被关注对象类型
	 * @param watchableId 被关注对象ID
	 * @return 关注者列表
	 */
	@Override
	public List<Watcher> getWatchers(String watchableType, Long watchableId) {
		return watcherMapper.selectWatchersByObject(watchableType, watchableId);
	}
	
	/**
	 * 获取某个对象的关注者数量
	 * 
	 * @param watchableType 被关注对象类型
	 * @param watchableId 被关注对象ID
	 * @return 关注者数量
	 */
	@Override
	public int getWatcherCount(String watchableType, Long watchableId) {
		List<Watcher> watchers = watcherMapper.selectWatchersByObject(watchableType, watchableId);
		return watchers != null ? watchers.size() : 0;
	}

}
